﻿using System;

namespace Noea.TogSim.Model.Train.LocoDriver.States
{
    public class AccelerateState : LocoState
    {
        public AccelerateState()
            : base()
        {
            base._description = "Accelerate";
        }

        public override LocoState Handle(LocoDriver_StPattern ld)
        {
            if (Math.Abs(ld.Train.ActualSpeed - ld.Train.RequestedSpeed) > 0.2)
            {
                double acc = Math.Abs(ld.Train.RequestedSpeed - ld.Train.ActualSpeed) / 2;
                if (ld.Train.ActualSpeed > ld.Train.RequestedSpeed)
                {
                    ld.Train.Engine.Acceleration = (ld.Train.Deacceleration < acc) ? -ld.Deacceleration : -acc;
                }
                else
                {
                    ld.Train.Engine.Acceleration = (ld.Acceleration < acc) ? ld.Acceleration : acc;
                }
                return this;
            }
            else
            {
                ld.Train.Engine.Acceleration = 0.0;
                ld.Train.ActualSpeed = ld.Train.RequestedSpeed;
            }
            return ld.PreviousState;
        }
    }
}
